import { QuerySignCallback, dataServerOpenDataView, updateDataView, sendBusinessMessage, FileServerProxy } from '@/utils/api'
import { GJDBFunction2 } from '@/utils/queryData'
import { getTaskName } from '@/utils/node_guid.js'
export const useSend = async (rowData, callBack) => {
  //图集发送三方
  //先获取需要发送的接口
  //rowData.VILLAGE当前村citycode
  // 资格：{417EFF61-5FA8-44D6-A624-5B3D0F39B56C}
  // 规划：{0439EC7A-785D-4B5B-ADD4-11E2D320376E}
  // 农房：{5821E9EB-44B5-49AC-A659-6DF98B410B38}
  //现场放样wfGuid：{AEA22B60-9184-42ED-9B8D-CEAE4A8F3021}
  //先获取当前环节
  const HJNAME = getTaskName(rowData.NODE_GUID)
  let conformityPerson = []
  let maxtnid = 0
  console.log(rowData, 'rowData')
  const person_Data = await GJDBFunction2('up_townaudit', [{ I_USERID: '330112002001', I_TYPE: 31 }])
  if (person_Data.length == 0) {
    this.$toast.error('未获取到对应村镇管理人员')
  }
  console.log(person_Data, 'conformityPerson')
  person_Data.forEach((zgjbr) => {
    let newItem = {}
    maxtnid = maxtnid + 1
    newItem.TNID = maxtnid
    newItem.PTNID = zgjbr.PNODEID
    newItem.TEXT = zgjbr.ST_NAME
    newItem.AUTOCHECKED = false
    newItem.wfNodeGuid = ''
    newItem.wfNodeType = 2
    newItem.USERID = zgjbr.ST_ID
    newItem.ISUSER = 1
    newItem.selected = true
    newItem.wfNodeName = '选图集'
    conformityPerson.push(newItem)
  })
  conformityPerson.forEach((item) => {
    if (item.PTNID == 100) {
      item.wfNodeGuid = '{417EFF61-5FA8-44D6-A624-5B3D0F39B56C}'
      item.wfNodeName = '镇街资格审核（经办人）'
    } else if (item.PTNID == 101) {
      item.wfNodeGuid = '{0439EC7A-785D-4B5B-ADD4-11E2D320376E}'
      item.wfNodeName = '镇街规划审核（经办人）'
    } else {
      item.wfNodeGuid = '{5821E9EB-44B5-49AC-A659-6DF98B410B38}'
      item.wfNodeName = '镇街农房审核（经办人）'
    }
  })

  //都要走这个接口
  let parms = {
    taskId: rowData.TASK_ID,
    direction: 0,
    wfGuid: rowData.WF_GUID,
    wfBoxGuid: 'd57762b5-7658-4131-b0db-30fd6909d2eb',
    sendNodes: JSON.stringify(conformityPerson),
    actionText: '同意',
    taskName: '浙里办',
    token: window.localStorage.getItem('token')
  }
  let params10 = {
    accessCode: 'c52bcf7cf3fb42bb9fbcd9b91e97d411',
    cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
    params: parms
  }
  const res = await QuerySignCallback(params10)
  if (eval('(' + res.data.data + ')').Success == true) {
    //成功调用回调
    callBack()
  }
}
export const wfGuid1 = (nodeGuid) => {
  if (nodeGuid === '{F81FCB82-7196-4E94-BFF5-159E1CEBEFF8}' || nodeGuid === '{FB74960E-0585-4B35-842F-D5DDEF3E00F3}') {
    //基槽验线
    return {
      wfGuid: '{857DCF34-4420-4140-B320-CC24888D968B}',
      wfNodeGuid: '{D9C76DDF-7A07-4F5F-A9F2-FD597C715481}',
      I_TYPE: 9,
      wfNodeName: '基槽验线'
    }
  } else if (nodeGuid === '{E349B5CD-E963-4A17-ABE4-258EF79C55F2}' || nodeGuid === '{FEF0E7B0-F048-42E0-998E-06387B0D03DB}') {
    //现场放样
    return {
      wfGuid: '{56B4962B-D338-4FA8-97B0-D6DF55EE4E41}',
      wfNodeGuid: '{646A4D45-00AC-4EA9-98AE-C5E704740BC1}',
      I_TYPE: 9,
      wfNodeName: '现场放样'
    }
  } else if (nodeGuid === '{10BAEA52-EDDE-48C7-97AD-C211D272010E}') {
    //一层立模
    return {
      wfGuid: '{00D4551A-0BA0-4073-A91B-0CCAB467F0D2}',
      wfNodeGuid: '{BFF3BE52-75D4-4C53-B5E9-9144E35E7617}',
      I_TYPE: 12,
      wfNodeName: '一层立模'
    }
  } else if (nodeGuid === '{C89D1E12-1303-482F-BB9E-45457905C759}') {
    //竣工验收
    return {
      wfGuid: '{879656FA-865D-445C-981D-FDA67C5454A4}',
      wfNodeGuid: '',
      I_TYPE: [
        {
          name: '镇街农房负责人',
          value: 17,
          wfNodeGuid:'{38E4E7DB-963D-40F1-B1E1-FF8793AD23AD}'
        },
        {
          name: '镇街规划经办人',
          value: 15,
          wfNodeGuid:'{74864380-8590-4F8B-BD75-942E08F04D81}'
        },
        {
          name: '镇街资格经办人',
          value: 13,
          wfNodeGuid:'{5CC13346-8C53-4A37-989D-932716248754}'
        }
      ],
      wfNodeName: '竣工验收'
    }
  } else if (nodeGuid === '{1055401E-8D9C-4AA8-AE67-394146C627FA}') {
    return '图集选用'
  } else if (nodeGuid === '{FEF0E7B0-F048-42E0-998E-06387B0D03DB}' || nodeGuid === '{9766D96E-8B85-481F-B514-C0B21CF4DC3A}') {
    return '选择施工单位&&合同签订'
  } else {
    this.$toast.fail('未知的节点')
  }
}
export const useSend2 = async (rowData, callBack, errorBack) => {
  console.log(rowData.NODE_GUID, 'byubvuyb')
  const wfGuid = wfGuid1(rowData.NODE_GUID)
  console.log(wfGuid.wfGuid, 'byubvuyb')
  // 创建流程接口
  let NHApplyParam = {
    accessCode: '2c824ce489ff4cffaa0d77082e6fb648',
    cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
    params: {
      wfGuid: wfGuid.wfGuid,
      entityRowGuid: rowData.ROW_GUID
    }
  }
  const NHApplyresData = await FileServerProxy(NHApplyParam)
  let SendFNHApplyresData = JSON.parse(NHApplyresData.data.data)
  if (SendFNHApplyresData.ErrorMsg == '没有发起流程的权限！' || SendFNHApplyresData.ErrorCode == 'wf-001') {
    this.$toast.fail(SendFNHApplyresData.ErrorMsg)
    return false
  }
  //获取人员信息
  var person_Data = []
  if (Array.isArray(wfGuid.I_TYPE)) {
     const promise =  wfGuid.I_TYPE.map(async (item) => {
      const person_Datas = await GJDBFunction2('up_townaudit', [{ I_USERID: '330112002001', I_TYPE: item.value }])
      if (person_Datas.length == 0) {
        errorBack()
        return false
      }
      person_Datas.forEach(items=>{
        items.wfNodeGuid = item.wfNodeGuid
      })
      return person_Datas
    })
    let p = Promise.all(promise)
    p.then((res)=>{
      res.forEach(item=>{
        person_Data = person_Data.concat(item)
      })
      // console.log(person_Data,'bperson_Data')
      sendNodes(rowData,person_Data, wfGuid, callBack, errorBack)
    })
  } else {
    person_Data = await GJDBFunction2('up_townaudit', [{ I_USERID: '330112002001', I_TYPE: wfGuid.I_TYPE }])
    sendNodes(rowData,person_Data, wfGuid, callBack, errorBack)
  }
}

const sendNodes = async (rowData,person_Data, wfGuid, callBack, errorBack) => {
  let maxtnid = 0
  let conformityPerson = []
  if (person_Data.length == 0) {
    errorBack()
    return false
  }
  person_Data.forEach((zgjbr) => {
    let newItem = {}
    maxtnid = maxtnid + 1
    newItem.TNID = maxtnid
    newItem.PTNID = zgjbr.PNODEID
    newItem.TEXT = zgjbr.ST_NAME
    newItem.AUTOCHECKED = false
    newItem.wfNodeGuid = wfGuid.wfNodeGuid? wfGuid.wfNodeGuid : zgjbr.wfNodeGuid
    newItem.wfNodeType = 2
    newItem.USERID = zgjbr.ST_ID
    newItem.ISUSER = 1
    newItem.selected = true
    newItem.wfNodeName = '镇街审核'
    conformityPerson.push(newItem)
  })
  //任务信息
  let params = {
    accessCode: '3ec88931e2bf429d9ceadff8697b6772',
    cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
    params: {
      wfGuid: wfGuid.wfGuid,
      entityRowGuid: rowData.ROW_GUID,
      token: window.localStorage.getItem('token')
    }
  }
  const resCreate = await FileServerProxy(params)
  let resCreateData = JSON.parse(resCreate.data.data)
  let TASK_ID = resCreateData.TaskId
  //都要走这个接口
  let parms = {
    taskId: TASK_ID,
    direction: 0,
    wfGuid: wfGuid.wfGuid,
    wfBoxGuid: '',
    sendNodes: JSON.stringify(conformityPerson),
    actionText: '同意',
    taskName: '浙里办',
    token: window.localStorage.getItem('token')
  }
  let params10 = {
    accessCode: 'c52bcf7cf3fb42bb9fbcd9b91e97d411',
    cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
    params: parms
  }
  const res = await QuerySignCallback(params10)
  if (eval('(' + res.data.data + ')').Success == true) {
    //成功调用回调 暂时不用发短信
    // let paerssend = {
    //   procName: 'message',
    //   dtParams: JSON.stringify([{ I_USERID: row, I_TYPE: 3 }]),
    //   token: window.localStorage.getItem('token')
    // }
    // let paramsendMEssage = {
    //   accessCode: '69db6bece3a7480592e2ec753d1e335e',

    //   cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
    //   params: paerssend
    // }
    // const reqwmsg = await QuerySignCallback(paramsendMEssage)
    // const JSONSend = JSON.parse(reqwmsg.data.data)
    // if (JSONSend && JSONSend.Data && JSONSend.Data.length > 0) {
    //   JSONSend.Data.forEach((phone) => {
    //     let paramdf = {
    //       mobiles: phone.MOBILETEL,
    //       templateId: '9a891424f9884d75b19163a95d8a5094',

    //       accessCode: '11b5cfe531954d94bb924c856e63a99b'
    //     }
    //     // 发送短信通知
    //     sendBusinessMessage(paramdf)
    //   })
    // }
    callBack()
  } else {
    errorBack()
  }
}
